package vista;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Vector;

import javabean.JPanelImagen;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.WindowConstants;
import bean.Cronometro;
import controlador.cUsuario;


import modelo.dto.Jurado;
import modelo.dto.Nivel;
import modelo.dto.Pelota;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class vMastermind extends javax.swing.JFrame {

	private Cronometro btnCronometro;

	
	private Nivel nivel=new Nivel();
	private Vector<Pelota> pelotas= new Vector<Pelota>();
	private Vector<Pelota> clavePelotas=new Vector<Pelota>();
	private ArrayList<Jurado> turnos= new ArrayList<Jurado>();
	
	private JButton btnComprobar;
	private JButton btnNuevojuego;
	private JButton btnsalir;
	private JButton btnPuntuacion;
	private JLabel lblCronometro;
	private JPanel panClave;
	private JPanelImagen panFondo;
	private JPanel panTablero;
	private JSeparator separador;
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public vMastermind(Nivel nivel) {
		super();
		this.nivel=nivel;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			BorderLayout thisLayout = new BorderLayout();
			getContentPane().setLayout(thisLayout);
			{
				panFondo = new JPanelImagen();
				getContentPane().add(panFondo, BorderLayout.CENTER);
				panFondo.setRutaImagen("/imagenes/niveles.jpg");
				panFondo.setSize(577, 741);
				panFondo.setPreferredSize(new java.awt.Dimension(721, 720));
				{
					panTablero = new JPanel(){
						public void paint(Graphics g)
					    {
							super.paint(g);
							for (Pelota p:pelotas){
								p.dibujar(g);
							}
					
							for (int i=0;i<turnos.size();i++){
						        if (i<nivel.intentos()){
						            g.drawString(Integer.toString(1+i),
						            		    5, 95+i*40);
						            turnos.get(i).pintar(g, 20, 70+i*40);
						        }
							}
					       }
					};
					panFondo.add(panTablero);
					panTablero.setLayout(null);
					
					/**
					 * Cambia el Bounds del panel Tablero
					 */
					if(nivel.getDificultad()==4)
					{
					panTablero.setBounds(197, 30, 250, 580);
					}else if(nivel.getDificultad()==5)
					{
						panTablero.setBounds(197, 30, 290, 600);
					}else{
						panTablero.setBounds(197, 12, 335, 630);
					}
					panTablero.setBackground(new java.awt.Color(255,255,255));
					panTablero.setLayout(null);
					{
						separador = new JSeparator();
						panTablero.add(separador);
						separador.setBounds(0, 53, 335, 15);
						separador.setBackground(new java.awt.Color(0,0,0));
						separador.setForeground(new java.awt.Color(0,0,0));
						separador.setBorder(BorderFactory.createCompoundBorder(
								null, 
								null));
					} 
				}
				{
					lblCronometro = new JLabel();
					panFondo.add(lblCronometro);
					lblCronometro.setText("Tiempo:");
					lblCronometro.setFont(new java.awt.Font("Segoe UI",1,14));
					lblCronometro.setForeground(new java.awt.Color(128,64,0));
					lblCronometro.setBounds(37, 96, 55, 16);
				}
				{
					panClave = new JPanel(){
						public void paint(Graphics g)
						{
							super.paint(g);
							for (Pelota p:clavePelotas) 
								p.dibujar(g);
						}
					};
					panFondo.add(panClave);
					panClave.setLayout(null);
					if(nivel.getDificultad()==4)
					{
					panClave.setBounds(197, 648, 250, 44);
					}else if(nivel.getDificultad()==5)
					{
						panClave.setBounds(197, 648, 290, 44);
					}else{
						panClave.setBounds(197, 648, 335, 44);
					}
					//panClave.setBounds(197, 648, 335, 44);
					panClave.setBackground(new java.awt.Color(255,255,255));
					panClave.setVisible(false);
				}
				{
					btnNuevojuego = new JButton();
					panFondo.add(btnNuevojuego);
					btnNuevojuego.setText("Nuevo Juego");
					btnNuevojuego.setFont(new java.awt.Font("Segoe UI",1,12));
					btnNuevojuego.setForeground(new java.awt.Color(128,64,0));
					btnNuevojuego.setBounds(27, 33, 144, 37);
				}
				{
					btnComprobar = new JButton();
					panFondo.add(btnComprobar);
					btnComprobar.setText("Comprobar");
					btnComprobar.setFont(new java.awt.Font("Segoe UI",1,12));
					btnComprobar.setForeground(new java.awt.Color(128,64,0));
					btnComprobar.setBounds(556, 42, 144, 34);
				}
				{
					btnsalir = new JButton();
					panFondo.add(btnsalir);
					btnsalir.setText("Salir");
					btnsalir.setBounds(593, 657, 117, 35);
					btnsalir.setForeground(new java.awt.Color(128,64,0));
					btnsalir.setFont(new java.awt.Font("Segoe UI",1,12));
				}
				{
					btnPuntuacion = new JButton();
					panFondo.add(btnPuntuacion);
					btnPuntuacion.setText("Puntuacion");
					btnPuntuacion.setBounds(34, 245, 144, 43);
					btnPuntuacion.setFont(new java.awt.Font("Segoe UI",1,12));
					btnPuntuacion.setForeground(new java.awt.Color(128,64,0));
				}
				{
					btnCronometro = new Cronometro();
					panFondo.add(btnCronometro);
					btnCronometro.setText("00:00");
					btnCronometro.setBounds(34, 135, 144, 50);
					btnCronometro.setFont(new java.awt.Font("Segoe UI",1,20));
					btnCronometro.setForeground(new java.awt.Color(128,64,0));
				}

			}
			pack();
			this.setSize(737, 741);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
		tiraColores();
		tiraClave();
	}
	
	public void tiraColores()
	{ 
		for(int i=0; i<nivel.getDificultad(); i++){
			pelotas.add(new Pelota (20+40*i, 15, 30));
		}
		
		
	}
	
	public void tiraClave()
	{
		for(int i=0; i<nivel.getDificultad();i++){
			
			clavePelotas.add(new Pelota(20+40*i, 5, 30, 
				         nivel.vectorclave().get(i)));	
		}
	}
	
	public void PresionarPelota(MouseEvent e)
	{
		 int x = e.getX();
	      int y = e.getY();
	      for (Pelota b:pelotas)
	        if (b.ubicacionclic(x, y)) 
	          b.cambiarColor();
	      repaint();
	}
	
	public void agregarListener(ActionListener accion)
	{
		btnPuntuacion.addActionListener(accion);
		btnComprobar.addActionListener(accion);
		btnsalir.addActionListener(accion);
		btnNuevojuego.addActionListener(accion);
	}
	
	public void agregarMouseListener(MouseListener me)
	{
		panTablero.addMouseListener(me);
	}

	
	public void nuevojuego(Nivel nivel)
	{   btnCronometro.reiniciar();
		this.nivel=nivel;
		turnos.removeAll(turnos);
		btnComprobar.setEnabled(true);
		panClave.setVisible(false);
			tiraClave();
		repaint();
	}
	
	
	public void PresionarComprobar()
	{
		 boolean terminoJuego=false;
    	 Vector<Integer> jugada=new Vector<Integer>();
         for(int i=0;i<pelotas.size();i++){
        	 jugada.add(pelotas.get(i).getColoractual());
         }
         Jurado jurado=new Jurado(nivel, jugada); 
         turnos.add(jurado);
         repaint();
         if (jurado.getPosCorrecta()==nivel.getDificultad()) 
          {
        	 btnCronometro.detenerse();
        	 terminoJuego=true;
        	 new cUsuario(nivel, btnCronometro.getText(),btnCronometro.getMinutos(),btnCronometro.getSegundos());
        	 btnComprobar.setEnabled(false);
        	 panClave.setVisible(true);
        	 // JOptionPane.showMessageDialog(this, "Felicitaciones has Ganado");
        	
          }else if(turnos.size()==nivel.intentos())
          {
        	  terminoJuego=true;
        	  btnCronometro.detenerse();
        	  JOptionPane.showMessageDialog(this, "Has Perdido");
        	  btnComprobar.setEnabled(false);
        	  panClave.setVisible(true);
          }
         
         if(!terminoJuego){
        	 btnCronometro.iniciar(); 
         }
         
	}
	public void Iniciar(){
		btnCronometro.detenerse();
	}
	public void Mensaje(String texto)
	{
		JOptionPane.showMessageDialog(this, texto);
	}
}
